package com.LeeCode;

import java.util.ArrayList;
import java.util.List;

/**
 * 二叉树展开为链表
 */

public class Code114 {

    List<TreeNode> list = new ArrayList<>();

    public static void main(String[] args) {

    }

    private TreeNode head;

    public void flatten(TreeNode root) {
        if (root == null) return;
        flatten(root.right);
        flatten(root.left);
        root.left = null;
        root.right = head;
        head = root;
    }

    public void flatten1(TreeNode root) {
        if (root == null) {
            return;
        }
        preorder(root);
        for (int i = 0; i < list.size() - 1; i++) {
            list.get(i).right = list.get(i + 1);
            list.get(i).left = null;
        }
    }

    public void preorder(TreeNode root) {
        if (root == null) {
            return;
        }
        list.add(root);
        preorder(root.left);
        preorder(root.right);
    }


}
